import Vue from "vue";
import Router from "vue-router";
import Apis from "@/apis";
// 懒加载路由并命名 chunk 文件
const Login = () => import(/* webpackChunkName: "group-foo" */ "@/pages/login");
const Signup = () =>
  import(/* webpackChunkName: "group-foo" */ "@/pages/signup");

const Main = () => import(/* webpackChunkName: "group-foo" */ "@/pages/main");
// 暴雨概率预测洪水
const rainsstorm = () => import("@/pages/unity/rainsstorm");
// 暴雨量预测洪水
const precipitation = () => import("@/pages/unity/precipitation");
// 水位计
const waterLevel = () => import("@/pages/unity/waterLevel");
// 流量计
const flow = () => import("@/pages/unity/flow");
// 重点建筑物网页介绍
const building = () => import("@/pages/unity/building");
// 值班人员
const dutyPersonList = () => import("@/pages/unity/dutyPersonList");
// 设备监测
const equipmentMonitoring = () => import("@/pages/unity/equipmentMonitoring");
// damSeepage
const damSeepage = () => import("@/pages/unity/damSeepage");
// 大坝变形
const damDeformation = () => import("@/pages/unity/damDeformation");
// 放水洞倾斜
const waterproofHole = () => import("@/pages/unity/waterproofHole");

// root 超级管理员
export const root = {
  path: "/root",
  component: Main,
  children: []
};

// admin 平台管理员
export const admin = {
  path: "/admin",
  component: Main,
  children: []
};

// goods 运维角色1
export const goods = {
  path: "/goods",
  component: Main,
  children: []
};

// bonus 运维角色2
export const bonus = {
  path: "/bonus",
  component: Main,
  children: []
};

// media 运维角色3
export const media = {
  path: "/media",
  component: Main,
  children: []
};
// operations 平台运维员
export const operations = {
  path: "/operations",
  component: Main,
  children: []
};
// salesman 业务员
export const salesman = {
  path: "/salesman",
  component: Main,
  children: []
};
// agent 代理商
export const agent = {
  path: "/agent",
  component: Main,
  children: []
};
// planner 规划师
export const planner = {
  path: "/planner",
  component: Main,
  children: []
};
// consult 咨询师
export const consultant = {
  path: "/consultant",
  component: Main,
  children: []
};
// consumer 消费者
export const consumer = {
  path: "/consumer",
  component: Main,
  children: []
};

// noinspection JSAnnotator
export const constantRouterMap = [
  {
    path: "/",
    // redirect: "login"
    redirect: "/unity/index"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/signup",
    component: Signup
  },
  {
    path: "/unity/index",
    component: () => import("@/pages/unity/index.vue")
  },
  {
    path: "/unity/preview",
    component: () => import("@/pages/unity/preview.vue")
  },
  {
    path: "/unity/rainsstorm",
    component: rainsstorm
  },
  {
    path: "/unity/precipitation",
    component: precipitation
  },
  {
    path: "/unity/waterLevel",
    component: waterLevel
  },
  {
    path: "/unity/flow",
    component: flow
  },
  {
    path: "/unity/building",
    component: building
  },
  {
    path: "/unity/dutyPersonList",
    component: dutyPersonList
  },
  {
    path: "/unity/equipmentMonitoring",
    component: equipmentMonitoring
  },
  {
    path: "/unity/damSeepage",
    component: damSeepage
  },
  {
    path: "/unity/damDeformation",
    component: damDeformation
  },
  {
    path: "/unity/waterproofHole",
    component: waterproofHole
  },

  // 管理
  root,
  admin,
  operations,
  salesman,
  agent,
  planner,
  consumer,
  consultant
];

Vue.use(Router);
const router = new Router({
  // 使用h5模式
  // mode: 'history',
  routes: constantRouterMap
});
router.beforeEach((to, from, next) => {
  // 获取权限名称  路由切换后判断当前路由地址是否存在 权限名称
  // 登录控制逻辑，根据项目不同做修改
  let user = false;
  if (sessionStorage.getItem(Apis.getLocalKey().user)) {
    user = JSON.parse(sessionStorage.getItem(Apis.getLocalKey().user));
  }
  // console.log(to,from)
  // 存路径
  sessionStorage.setItem("to_path", to.path);
  if (to.path.indexOf("/unity") != -1) {
    next();
  } else if (to.path !== "/login" && to.path !== "/signup" && !user) {
    next({
      // path: "/"
      // path: "/unity/engineer_control_left"
      path: "/unity/index"
    });
  } else {
    next();
  }
});

export default router;
